/**
 * @author yzhou2
 */
var GLASettingPanel=function(container,config,callback){
	var t=QS_Lang.marketGLA
    this.container=container;
    this.config={
        counrty:'USA',
        exchange:'126',
        type:'tab'
    };
    this.callback={};
    if(config){
        $.extend(this.config,config);
    }
    if(callback){
        $.extend(this.callback,callback);
    }
    this.html='<div class="qs-gla-setting"><h5>'+t.country+'</h5><div class="qs-setting-ddl clearfix qs-gla-setting-country"></div><h5>'+t.exchange+'</h5><div class="qs-setting-ddl clearfix qs-gla-setting-exchange"></div><h5>'+t.type+'</h5><div class="qs-setting-ddl clearfix qs-gla-setting-type"></div></div>'
    this.el=null;
    this.countryData=[{
        val:'USA',
        txt:t.usa
    },{
        val:'CAN',
        txt:t.can
    }];
    this.exchangeData={
        'USA':[{txt:t.comp,val:"126"},
            {txt:t.nyse,val:"14"},
            {txt:t.nasdaq,val:"19"},
            {txt:t.amex,val:"1"},
            {txt:t.arca,val:"16"}],
        'CAN':[{txt:t.comp,val:"127"},
                {txt:t.toronto,val:"127.1"},
                {txt:t.venture,val: "127.2"}]
    };
    this.typeData=[{
        val:'tab',
        txt:t.tab
    },{
        val:'list',
        txt:t.list
    }];
    this.countryDDL=null;
    this.exchangeDDL=null;
    this.typeDDL=null;
    this.exchange=null;
    this.ddlWidth=142;
    this.init();
};

GLASettingPanel.prototype={
    init:function(){
        this.el=$(this.html).appendTo(this.container);
        this._createExchangeDDL(this.el.find('.qs-gla-setting-exchange'));
        this._createTypeDDL(this.el.find('.qs-gla-setting-type'));
        this._createCountryDDL(this.el.find('.qs-gla-setting-country'));
    },
    _changeExchangeByCountry:function(country){
        var exchangeData=this.exchangeData[country];
        var val=exchangeData[0].val;
        for(var i=0,l=exchangeData.length;i<l;i++){
            if(exchangeData[i].val==this.config.exchange){
                val=exchangeData[i].val;
                break;
            }
        }
        this.exchangeDDL.selectItemByValue(val);
        this.config.exchange=val;
    },
    _createCountryDDL:function(container){
        var self=this;
        this.countryDDL=new Util.DropDownList(container,function(t, v){
            self.config.counrty=v;
            self.exchangeDDL.rebindData(self.exchangeData[v]);
            self._changeExchangeByCountry(v);
        },this.countryData,{
            actionAtInit:false,
            width:this.ddlWidth
        });
        this.countryDDL.selectItemByValue(this.config.counrty);
    },
    _createExchangeDDL:function(container){
        var self=this;
        this.exchangeDDL=new Util.DropDownList(container,function(t, v){
            if(self.exchange!=null){
                if(typeof self.callback.onChangeExchange=='function'){
                    self.callback.onChangeExchange(self.config.counrty,v);
                }
            }else{
                self.exchange=v;
            }
        },[],{
            actionAtInit:false,
            width:this.ddlWidth
        });
    },
    _createTypeDDL:function(container){
        var self=this;
        this.typeDDL=new Util.DropDownList(container,function(t, v){
            if(typeof self.callback.onChangeType=='function'){
                self.callback.onChangeType(v);
            }
        },this.typeData,{
            actionAtInit:false,
            width:this.ddlWidth
        });
        this.typeDDL.setTextValue(null,this.config.type);
    },
    clear:function(){
        for(var p in this){
            if(!p || p == 'clear'){continue;}
            this[p] = null;
            delete this[p];
        }
    }
};

